package com.aw.guiguzi.module.manager.msg.presenters

import com.aw.baselib.base.AwCommonPresenter
import com.aw.baselib.net.callback.AwApiCallback
import com.aw.guiguzi.bean.BasePageRspBean
import com.aw.guiguzi.bean.MsgTemplateItemBean
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.module.manager.msg.views.MsgCreateView
import com.aw.guiguzi.net.observers.NetObCommon

/**
 * @author : hzw
 */
class MsgCreatePresent(private val mView: MsgCreateView.View) : AwCommonPresenter(),
    MsgCreateView.Presenter {
    override fun getMsgTemplateList() {
        addIOSubscription<BasePageRspBean<MsgTemplateItemBean>>(
            NetObCommon.getMsgTemplateList(1),
            object : AwApiCallback<BasePageRspBean<MsgTemplateItemBean>>(mView, true, true) {
                override fun onSuccess(result: BasePageRspBean<MsgTemplateItemBean>) {
                    mView.getMsgTemplateListSuccess(result.total, result?.list ?: emptyList())
                    getUserListSimple()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    mView.getMsgTemplateListFail(code, msg)
                }
            })
    }

    override fun getUserListSimple() {
        addIOSubscription<List<UserInfoBean>>(
            NetObCommon.getUserListSimple(),
            object : AwApiCallback<List<UserInfoBean>>(mView, true, true) {
                override fun onSuccess(result: List<UserInfoBean>) {
                    mView.getUserListSimpleSuccess(result)
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    mView.getUserListSimpleFail(code, msg)
                }
            })
    }

    override fun msgCreateToSend(
        id: String?,
        templateId: String?,
        content: HashMap<String?, String?>?
    ) {
        addIOSubscription(
            NetObCommon.msgCreateToSend(id, templateId, content),
            object : AwApiCallback<String>(mView, true, true) {
                override fun onSuccess(result: String) {
                    mView.msgCreateToSendSuccess()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                }
            })
    }
}